package top.yehot.kettle.boot.service.impl;

import java.util.Optional;
import java.util.Properties;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import top.yehot.cmm.util.mybatis.utils.PageUtil;
import top.yehot.cmm.util.mybatis.utils.SpecialCharacterHandle;
import top.yehot.kettle.boot.enums.GlobalParamEnum;
import top.yehot.kettle.boot.mapper.GlobalParamMapper;
import top.yehot.kettle.boot.models.entity.GlobalParam;
import top.yehot.kettle.boot.models.vo.param.GlobalParamAddVO;
import top.yehot.kettle.boot.models.vo.param.GlobalParamEditVO;
import top.yehot.kettle.boot.models.vo.param.GlobalParamPageVO;
import top.yehot.kettle.boot.service.GlobalParamService;

/**
 * 参数 服务实现类
 * 
 * @author lquan
 * @date 2022年6月3日 下午4:50:03
 */
@Service
public class GlobalParamServiceImpl extends ServiceImpl<GlobalParamMapper, GlobalParam> implements GlobalParamService {

	@Override
	public IPage<GlobalParam> page(GlobalParamPageVO param) {
		LambdaQueryWrapper<GlobalParam> query = Wrappers.lambdaQuery(GlobalParam.class);
		query.in(GlobalParam::getCmmv, GlobalParamEnum.ENV.getValue(), GlobalParamEnum.GLOBAL.getValue());
		if (null != param.getCmmv()) {
			query.eq(GlobalParam::getCmmv, param.getCmmv().getValue());
		}
		if (StringUtils.hasText(param.getPkey())) {
			query.like(GlobalParam::getPkey, SpecialCharacterHandle.handleKeyword(param.getPkey()));
		}
		query.orderByDesc(GlobalParam::getCreateTime);

		return baseMapper.selectPage(PageUtil.of(param), query);
	}

	@Transactional
	@Override
	public void add(GlobalParamAddVO param) {
		// 拷贝对象
		GlobalParam entity = new GlobalParam();
		BeanUtils.copyProperties(param, entity);
		// 设置通用
		entity.setCmmv(param.getCmmv().getValue());
		// 保存
		baseMapper.insert(entity);
		
		// 设置环境变量
		if (GlobalParamEnum.ENV.getValue().equals(entity.getCmmv())) {
			Properties prop = System.getProperties();
			prop.remove(entity.getPkey());
			prop.put(entity.getPkey(), entity.getPvalue());
		}
	}

	@Transactional
	@Override
	public void edit(GlobalParamEditVO param) {
		// 查询旧对象
		GlobalParam oldEntity = baseMapper.selectById(param.getId());
		
		// 拷贝对象
		GlobalParam newEntity = new GlobalParam();
		BeanUtils.copyProperties(param, newEntity);
		// 设置通用
		newEntity.setCmmv(param.getCmmv().getValue());
		// 保存
		baseMapper.updateById(newEntity);
		
		// 设置环境变量
		Properties prop = System.getProperties();
		if (GlobalParamEnum.ENV.getValue().equals(oldEntity.getCmmv())) {
			prop.remove(oldEntity.getPkey());
		}
		if (GlobalParamEnum.ENV.getValue().equals(newEntity.getCmmv())) {
			prop.put(newEntity.getPkey(), newEntity.getPvalue());
		}
	}

	@Transactional
	@Override
	public void delete(Integer id) {
		GlobalParam param = baseMapper.selectById(id);
		Optional.ofNullable(param).ifPresent(p -> {
			if (GlobalParamEnum.ENV.getValue().equals(p.getCmmv())) {
				System.getProperties().remove(param.getPkey());
			}
		});
		baseMapper.deleteById(id);
	}

}
